import json
import time
from datetime import datetime

import requests


def nowTime():
    return datetime.today().strftime("%Y-%m-%d %H:%M:%S")


urlFormat = 'http://domp.bluemoon.com.cn/daps-system-biz/dap/v1.0/system/login?username={}&password={}&captcha={}'
url = urlFormat.format('80560490', 'kdg123456', 'q')
f = open('login-api-request.txt', 'w', encoding='utf8')
f.write("模拟调用接口：" + urlFormat.format('80560490', 'xxxx', 'q') + "\n")
start = datetime.now()
f.write("开始时间：" + nowTime() + "\n")
f.flush()
i, n = 0, 1000
slowReqCount, maxShowFlag = 0, 1.0
while i <= n:
    r = requests.post(url)
    dictInfo = json.loads(r.text)
    cost = str(dictInfo['cost']).replace("PT", '').lower().replace('s', '')
    print(cost + 's')
    f.write(cost + "s\n")
    if float(cost) > maxShowFlag:
        slowReqCount += 1
    f.flush()
    time.sleep(0.5)
    i += 1

end = datetime.now()
f.write("结束时间：{}，总耗时{}秒（其中每次调用睡眠{}毫秒），耗时大于{}秒为{}次".format(nowTime(), str((end - start).seconds), 500, str(maxShowFlag),
                                                          str(slowReqCount)))
f.flush()
f.close()
print("结束时间：" + nowTime())
print('end')
